package com.servlet;
import com.entity.Admin;
import com.entity.Flight;
import com.entity.OrderTicket;
import com.entity.UserInfo;
import com.service.Queryables;
import com.service.cairns;
import com.util.DataGrideView;
import com.util.DataGrideView1;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
 public  class  Import {

    private Queryables queryables;
    @Autowired
    public void setQueryables(Queryables queryables) {
        this.queryables = queryables;
    }

    private cairns  cairns;
    @Autowired
    public void setCairns(cairns cairns) {
        this.cairns = cairns;
    }

    @RequestMapping("user")
    public String aa() {
        return "user";
    }



    @RequestMapping("/login")
    public String toIndex1(UserInfo userInfo) {
        String username = userInfo.getLoginName();
        String password = userInfo.getLoginPassword();
        return getString(username,password);
    }

    @RequestMapping("/login1")
    public String toIndex(Admin admin) {
        String username = admin.getAdmUsername();
        String password = admin.getAdmPassword();
        return getString(username,password);
    }

    private String getString(String username , String password) {
        String  qwe ="";
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        String msg = null;
        try {
            // 登录验证，验证失败会抛出异常，可以在 userService.login 中抛出，这里进行捕获
            subject.login(token);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            msg = e.getMessage();
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            msg = "用户名和密码不匹配";
        } catch (LockedAccountException e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        if (msg == null) {
            if (username.length() == 5) {
                qwe="user";
            }
            if (username.length() != 5) {
                qwe="admin";
            }
        } else {
            qwe = "index";
        }
        return  qwe;
    }


    @RequestMapping("admin")
    public String bb() {
        return "admin";
    }

    @RequestMapping("sss")
    public String cc() {
        return "index";
    }

    @ResponseBody
    @RequestMapping("ForSelect")
    public DataGrideView1 loadAll1(){
        List<Flight> lists  = queryables.Query();
        return new DataGrideView1(lists);
    }

    @ResponseBody
    @RequestMapping("loadAllCustomerForSelect1")
    public DataGrideView loadAllProviderForSelect1() {
        List<OrderTicket> list  = queryables.QueryDingDong();
        return new DataGrideView(list);
    }

    @ResponseBody
    @RequestMapping("load")
    public DataGrideView load() {
          Object ss = SecurityUtils.getSubject().getPrincipal();
          List<OrderTicket> list  = cairns.self((String) ss);
          return new DataGrideView(list);
    }

    @RequestMapping("out")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout(); // session 会销毁，在SessionListener监听session销毁，清理权限缓存
        }
        return "index";
    }


}